package thinkingJava.chapter17;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class Lists {
	private static boolean b ;
	private static String s ;
	private static int i ;
	private static Iterator<String> it ;
	private static ListIterator<String> lit ;
	public static void basicTest(List<String> a) {
		a.add(1, "x");
		a.add("x");
		a.addAll(Countries.names(25));
		a.addAll(3, Countries.names(25));
		b = a.contains("1");
		b = a.containsAll(Countries.names(25));
		s = a.get(1);
		i = a.indexOf("1");
		b = a.isEmpty();
		it = a.iterator();
		lit = a.listIterator();
		lit = a.listIterator(3);
		i = a.lastIndexOf("1");
		a.remove(1);
		a.remove("3");
		a.set(1, "y");
		a.retainAll(Countries.names(25));
		a.removeAll(Countries.names(25));
		i = a.size();
		a.clear();
	}
	public static void iterMotion(List<String> a) {
		ListIterator<String> lit = a.listIterator();
		b = lit.hasNext();
		b = lit.hasPrevious();
		s = lit.next();
		i = lit.nextIndex();
		s = lit.previous();
		i = lit.previousIndex();
	}
	public static void iterManipulation(List<String> a) {
		ListIterator<String> lit = a.listIterator();
		lit.add("47");
		lit.next();
		lit.remove();
		lit.next();
		lit.set("47");
	}
	public static void testVisual(List<String> a) {
		print(a);
		List<String> b = Countries.names(25);
		print("b = " + b);
		a.addAll(b);
		a.addAll(b);
		print(a);
		ListIterator<String> x = a.listIterator(a.size() / 2);
		x.add("one");
		print(a);
		print(x.next());
		x.remove();
		print(x.next());
		x.set("47");
		print(a);
		x = a.listIterator(a.size());
		while (x.hasPrevious()) {
			print(x.previous() + " ");
		}
		print("");
		print("testVisual finished!");
	}
	public static void testLinkedList() {
		LinkedList<String> ll = new LinkedList<String>();
		ll.addAll(Countries.names(25));
		print(ll);
		ll.addFirst("one");
		ll.addFirst("two");
		print(ll);
		print(ll.getFirst());
		print(ll.removeFirst());
		print(ll.removeFirst());
		print(ll.removeLast());
		print(ll);
	}
	public static void main(String[] args) {
		basicTest(
				new LinkedList<String>(Countries.names(25)));
		basicTest(
				new ArrayList<String>(Countries.names(25)));
		iterMotion(
				new LinkedList<String>(Countries.names(25)));
		iterMotion(
				new ArrayList<String>(Countries.names(25)));
		iterManipulation(
				new LinkedList<String>(Countries.names(25)));
		iterManipulation(
				new ArrayList<String>(Countries.names(25)));
		testVisual(
				new LinkedList<String>(Countries.names(25)));
//		basicTest(new ArrayList<String>(10));
		testLinkedList();
	}
	public static void print(Object o) {
		System.out.println(o);
	}
}
